import { Component } from "@angular/core"
import { App,LoadingController,AlertController,NavParams } from 'ionic-angular';
import { NativeStorage } from '@ionic-native/native-storage';
import { HttpClient } from '@angular/common/http'
import { TranslateService } from '@ngx-translate/core';

import { AppConfig } from '../../../app/app.config';
import { PostrentPage } from "../../home/mypost/rent/rent";
import { PostbuyPage } from "../../home/mypost/buy/buy";
import { PostperPage } from "../../home/mypost/per/per";
import { PostservicePage } from "../../home/mypost/service/service";
import { RenewlistPage } from "../../pay/renewlist/renewlist"
@Component({
    selector: 'page-postlist',
    templateUrl: 'postlist.html',
})

export class PostlistPage{
    list: any;
    loading:any
    src:string;
    token:string
    renewlistPage=RenewlistPage
    isRoot=false;
    constructor(
        private http:HttpClient,
        private stroge:NativeStorage,
        private loadingCtrl:LoadingController,
        public app:App,
        private alertCtrl:AlertController,
        private navParams:NavParams,
        private translate: TranslateService,
    ) {
        this.src = AppConfig.getMainUrl()
        this.isRoot = this.navParams.get('isRoot')?true:false
        translate.setDefaultLang('en')
    }
    ionViewDidEnter(){
        this.stroge.getItem('lang').then(res=>{
            this.translate.use(res)
        })
    }
    loadWait(){
        this.loading = this.loadingCtrl.create({
            content: 'Please wait...'
        });
        this.loading.present();
    }
    _cate(item:number){
        switch(item){
            case 1:
                return 'Rent'
            case 2:
                return 'Buy'
            case 3:
                return 'Rent and day'
            case 4:
                return 'Service'
        }
    }
    _type(item:number){
        switch(item){
            case 1:
                return 'month'
            case 2:
                return 'week'
            case 3:
                return 'day'
        }
    }
    _unit(item:number){
        switch(item){
            case 1:
                return '£'
            case 2:
                return '€'
            case 3:
                return '$'
            case 4:
                return '¥'
        }
    }
    _aunit(item:number){
        switch(item){
            case 1:
                return 'SQ.M'
            case 2:
                return 'SQ.FT'
        }
    }
    _otherArea(num,type){
        switch(type){
            case 1:
                return '('+Math.ceil(num/10.764)+'SQ.FT)'
            case 2:
                return '('+Math.ceil(num*10.764)+'SQ.M)'
        }
    }
    goDetail(item){
        switch(item.type){
            case 1:
            this.app.getRootNav().push(PostrentPage,{id:item.id})
                break
            case 2:
            this.app.getRootNav().push(PostbuyPage,{id:item.id})
                break
            case 3:
            this.app.getRootNav().push(PostperPage,{id:item.id})
                break
            case 4:
            this.app.getRootNav().push(PostservicePage,{id:item.id})
                break
        }
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    del(item){
        let url = '';
        switch(item.type){
            case 1:
            case 2:
            case 3:
                url = AppConfig.getUrl()+'house/delete?id='+item.id+'&token='+this.token
                break
            case 4:
                url = AppConfig.getUrl()+'service/delete?id='+item.id+'&token='+this.token
                break
        }
        let alert = this.alertCtrl.create({
            title: '',
            message: 'Confirm delete?',
            buttons: [
            {
                text: 'Cancel',
                role: 'cancel',
                handler: () => {
                    
                }
            },
            {
                text: 'Ok',
                handler: () => {
                    this.http.get(url)
                    .subscribe( (res) => {
                        if(res['code']===101){
                            this.list.splice(item.index,1);
                        }else {
                            this.alertb('Error!',res['msg']);
                        }
                    })
                }
            }
            ]
        });
        alert.present();
    }
    ionViewDidLoad(){
        this.stroge.getItem('token').then(token=>{
            this.token = token;
            this.loadWait()
            this.http.get(AppConfig.getUrl()+'user/getPosts?token='+token).subscribe(res=>{
                if(res['code']==101){
                    this.list = res['data']
                    this.loading.dismiss()
                }
            })
        })
    }
    goto(page){
        this.app.getRootNav().push(page)
    }
}